package fr.pioug.gdocs.service;

import java.io.*;
import java.util.*;

import com.google.gdata.util.*;

import fr.pioug.gdocs.gui.*;
import fr.pioug.gdocs.object.*;

/**
 * @author a011228
 * @version
 */
public class SyncOnServerService extends SyncService {
	private static SyncOnServerService mSyncService = null;

	/**
	 * @param pObserver
	 * @return
	 */
	public static SyncService getInstance(Observer pObserver) {
		if (mSyncService == null) {
			mSyncService = new SyncOnServerService();
			mSyncService.addObserver(pObserver);
		}
		return mSyncService;
	}

	protected void doJob(GDocsService pGDocsService) {
		if (pGDocsService != null) {
			LocalService lLocalService = LocalService.getInstance();
			// Work
			GDocsFile lLocalTree = lLocalService.getTree(ConstantesService
					.getInstance().getValue("conf.directory"));
			GDocsFile lServerTree = null;
			try {
				lServerTree = pGDocsService.getTree();
			} catch (IOException lException) {
				LogService.showError("error.treeLocalIo", GDocsSync.class,
						lException);
			} catch (ServiceException lException) {
				LogService.showError("error.treeLocalService", GDocsSync.class,
						lException);
			}

			GDocsFile lDocsFile = new GDocsFile(diff(lLocalTree, lServerTree));

			pGDocsService
					.postChanges(lDocsFile, null, lDocsFile.countNbModif());

		}
	}

}
